#include "thread_wrapper.h"
#include <iostream>
#include <memory>
#include <conio.h>

#import "Skype4COM.dll" named_guids


class ThreadWrapperImpl : public Common::ThreadWrapper
{
public:
	~ThreadWrapperImpl() {}
	virtual Common::Error Run()
	{
		while(!QueryExitStatus(0))
		{
			std::cout << "ping\n";
		}
		return Common::SKYPE_SUCCESS;
	}
};

class TestModule : public Common::Module
{
public:
	TestModule()
	{
		using namespace SkypeInstanceManagement;

		InstanceManagerParams params;
		params.dispatch_events = false;
		manager = CreateInstanceManager(params);
		if (!manager.get())
			throw Common::Exception("Unable to create manager");
	}

	~TestModule(){}

	// Module implementation
	virtual Common::Error BeforeRun(){ std::cout << "Before run" << std::endl; return Common::SKYPE_SUCCESS; }
	virtual Common::Error AfterRun(){ std::cout << "After run" << std::endl; return Common::SKYPE_SUCCESS; }

	// ThreadWrapper implementation
	virtual Common::Error RunLogic()
	{
		using namespace SKYPE4COMLib;

		ISkypePtr skype = manager->GetSkypeInstance();
		IChatCollectionPtr collection = skype->GetActiveChats();
		std::cout << "num active chats: " << collection->GetCount() << std::endl;
		for (int i = 1; i <= collection->GetCount(); ++i)
		{
			IChatPtr chat = collection->GetItem(1);
			std::cout << chat->GetName() << std::endl;
		}
		collection = 0;

		return Common::SKYPE_SUCCESS;
	}

private:
	std::auto_ptr<SkypeInstanceManagement::Manager> manager;
};


class TestThread : public Common::ThreadWrapper
{
public:
	TestThread()
	{
		using namespace SkypeInstanceManagement;

		InstanceManagerParams params;
		params.dispatch_events = false;
		manager = CreateInstanceManager(params);
		if (!manager.get())
			throw Common::Exception("Unable to create manager");
	}

	~TestThread() {}

	virtual Common::Error Run()
	{
		using namespace SkypeInstanceManagement;
		using namespace SKYPE4COMLib;

		/*InstanceManagerParams params;
		params.dispatch_events = false;

		std::auto_ptr<Manager> manager(CreateInstanceManager(params));
		if (!manager.get())
		{
			std::cout << "Unable to create instance manager" << std::endl;
			return Common::SKYPE_ERROR;
		}*/

		ISkypePtr skype = manager->GetSkypeInstance();
		IChatCollectionPtr collection = /*skype->GetActiveChats()*/skype->GetChats();
		std::cout << "num active chats: " << collection->GetCount() << std::endl;
		for (int i = 1; i <= collection->GetCount(); ++i)
		{
			IChatPtr chat = collection->GetItem(1);
			std::cout << chat->GetName() << std::endl;
		}
		collection = 0;

		/*while(!QueryExitStatus(0))
		{
			std::cout << "ping\n";
		}*/
		return Common::SKYPE_SUCCESS;
	}

private:
	std::auto_ptr<SkypeInstanceManagement::Manager> manager;
};


int main(int argc, char* argv[])
{
	/*using namespace SkypeInstanceManagement;
	using namespace SKYPE4COMLib;

	InstanceManagerParams params;
	//params.logger = m_Log;
	params.dispatch_events = false;

	std::auto_ptr<Manager> manager = CreateInstanceManager(params);
	if (!manager.get())
		std::cout << "ModuleThreadWrapper: ERROR - Unable to create instance manager" << std::endl;

	ISkypePtr skype = manager->GetSkypeInstance();

	IChatCollectionPtr collection = skype->GetActiveChats();
	std::cout << "num active chats: " << collection->Count << std::endl;
	collection = 0;*/

	try
	{
		std::auto_ptr<TestModule> module(new TestModule());
		module->Start();
		module->WaitStop();
	}
	catch(Common::Exception& ex)
	{	
		std::cout << "EXCEPTION: " << ex.GetStr().c_str() << std::endl;
	}
	
	getch();
}